package cetcbigdata.da.service.hainan;

import cetcbigdata.da.base.Base;
import cetcbigdata.da.service.hainan.util.AllInfo;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

/**
 * 政务公开
 * 最多跑一次
 */

public class ZuiDuoPaoYiCi extends Base {
    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {

    }

    @Override
    public JSONObject getDataDemo() {
        try {
            return parseListPage();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public JSONObject parseListPage(){
        //创建 详情页信息对象
        AllInfo allInfo = new AllInfo();
        allInfo.init();
        //当前页
        int currentPage = 1;
        //总页数
        int pages = 0;
        while (currentPage != pages){
            String listUrl = "https://wssp.hainan.gov.cn/wssp3/zwgk/zdpyc";
            JSONObject listHeader = new JSONObject();
            JSONObject listParmas = new JSONObject();

            listHeader.put("Referer", "https://wssp.hainan.gov.cn/hnwt/govpublic_detail2?qdCategory=zdpyc&title=%E6%9C%80%E5%A4%9A%E8%B7%91%E4%B8%80%E6%AC%A1");
            listHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");

            listParmas.put("regionId","HZ2881f4424539dd0142453c856b0025");
            listParmas.put("pageNumber",currentPage);
            listParmas.put("pageSize","10");
            listParmas.put("pdmsType","1");
            listParmas.put("qdCategory","zdpyc");

            String listStrResponse = postFormReq(listUrl,listHeader,listParmas);
            JSONObject listResponse = JSONObject.parseObject(listStrResponse);
            if (listResponse.getInteger("code") == 20000) {
                for (Object o : listResponse.getJSONObject("data").getJSONArray("datas")){
                    JSONObject object = (JSONObject) o;
                    JSONObject pageHeader = new JSONObject();
                    JSONObject pageParmas = new JSONObject();
                    allInfo.setAllInfoHeader(pageHeader);
                    allInfo.setAllInfoParmas(pageParmas);
                    pageHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
                    pageHeader.put("Referer", "https://wssp.hainan.gov.cn/hnwt/handlingGuideline?id="+object.getInteger("id"));
                    pageParmas.put("id", object.getInteger("id"));
                    JSONObject result;
                    try {
                        result = allInfo.getAllInfo();
                    } catch (Exception e) {
                        break;
                    }
                    System.out.println(result);
                    return processResult(
                            null,
                            null,
                            null,
                            allInfo.getResponce(),
                            result,
                            "海南省",
                            "政务公开-最多跑一次"
                    );
                }
                currentPage++;
                pages = listResponse.getJSONObject("data").getInteger("pageCount") + 1;
            }
        }
        return null;
    }

    public static void main(String[] args) {
        ZuiDuoPaoYiCi zuiDuoPaoYiCi = new ZuiDuoPaoYiCi();
        zuiDuoPaoYiCi.getDataDemo();
    }
}
